﻿/* QF classbuilder for Enlib */
using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;

#region SYS_Role实体类
public class SYS_Role
{
	private string m_RoleID;
	private string m_Name;
	private string m_Description;
	private bool m_Enabled;
	private DateTime m_LastModifiedTime;
	private string m_LastModifiedByID;
	
	public SYS_Role ()
	{
		m_RoleID="";
		m_Name="";
		m_Description="";
		m_Enabled=false;
		m_LastModifiedTime=DateTime.Now;
		m_LastModifiedByID="";
	}
	
	public string RoleID
	{
		get{return m_RoleID;}
		set{m_RoleID=value;}
	}
	public string Name
	{
		get{return m_Name;}
		set{m_Name=value;}
	}
	public string Description
	{
		get{return m_Description;}
		set{m_Description=value;}
	}
	public bool Enabled
	{
		get{return m_Enabled;}
		set{m_Enabled=value;}
	}
	public DateTime LastModifiedTime
	{
		get{return m_LastModifiedTime;}
		set{m_LastModifiedTime=value;}
	}
	public string LastModifiedByID
	{
		get{return m_LastModifiedByID;}
		set{m_LastModifiedByID=value;}
	}
}
#endregion

#region SYS_Role管理类
public partial class LogicLayer
{
	public bool AddSYS_Role(SYS_Role theSYS_Role)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "insert into SYS_Role (RoleID,Name,Description,Enabled,LastModifiedTime,LastModifiedByID) values (@RoleID,@Name,@Description,@Enabled,@LastModifiedTime,@LastModifiedByID)";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@RoleID", DbType.String, theSYS_Role.RoleID);
			db.AddInParameter(cmd, "@Name", DbType.String, theSYS_Role.Name);
			db.AddInParameter(cmd, "@Description", DbType.String, theSYS_Role.Description);
			db.AddInParameter(cmd, "@Enabled", DbType.Boolean, theSYS_Role.Enabled);
			db.AddInParameter(cmd, "@LastModifiedTime", DbType.DateTime, theSYS_Role.LastModifiedTime);
			db.AddInParameter(cmd, "@LastModifiedByID", DbType.String, theSYS_Role.LastModifiedByID);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("AddSYS_Role:" + ex.Message );
			return false;
		}
	}
	
	public SYS_Role GetSYS_RoleFromDataReader(IDataReader dataReader)
	{
		try
		{
			SYS_Role result = new SYS_Role();
			
			if (dataReader["RoleID"] != null && dataReader["RoleID"] != System.DBNull.Value )
				result.RoleID = (string)dataReader["RoleID"];
			if (dataReader["Name"] != null && dataReader["Name"] != System.DBNull.Value )
				result.Name = (string)dataReader["Name"];
			if (dataReader["Description"] != null && dataReader["Description"] != System.DBNull.Value )
				result.Description = (string)dataReader["Description"];
			if (dataReader["Enabled"] != null && dataReader["Enabled"] != System.DBNull.Value )
				result.Enabled = (bool)dataReader["Enabled"];
			if (dataReader["LastModifiedTime"] != null && dataReader["LastModifiedTime"] != System.DBNull.Value )
				result.LastModifiedTime = (DateTime)dataReader["LastModifiedTime"];
			if (dataReader["LastModifiedByID"] != null && dataReader["LastModifiedByID"] != System.DBNull.Value )
				result.LastModifiedByID = (string)dataReader["LastModifiedByID"];
			
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetReaderSYS_Role:" + ex.Message);
			return null;
		}
	}
	
	public SYS_Role GetSYS_Role(string RoleID)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * From SYS_Role where RoleID=@RoleID";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@RoleID", DbType.String, RoleID);
			
			SYS_Role result = null;
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
				{
					result = GetSYS_RoleFromDataReader( dataReader );
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetSYS_Role:" + ex.Message);
			return null;
		}
	}
	
	public bool DelSYS_Role(string RoleID)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "delete From SYS_Role where RoleID=@RoleID";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@RoleID", DbType.String, RoleID);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("DelSYS_Role:" + ex.Message);
			return false;
		}
	}
	
	public bool UpdateSYS_Role(SYS_Role theSYS_Role)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "update SYS_Role set Name=@Name,Description=@Description,Enabled=@Enabled,LastModifiedTime=@LastModifiedTime,LastModifiedByID=@LastModifiedByID where RoleID=@RoleID";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@Name", DbType.String, theSYS_Role.Name);
			db.AddInParameter(cmd, "@Description", DbType.String, theSYS_Role.Description);
			db.AddInParameter(cmd, "@Enabled", DbType.Boolean, theSYS_Role.Enabled);
			db.AddInParameter(cmd, "@LastModifiedTime", DbType.DateTime, theSYS_Role.LastModifiedTime);
			db.AddInParameter(cmd, "@LastModifiedByID", DbType.String, theSYS_Role.LastModifiedByID);
			db.AddInParameter(cmd, "@RoleID", DbType.String, theSYS_Role.RoleID);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("UpdateSYS_Role:" + ex.Message);
			return false;
		}
	}
	
	public List<SYS_Role> ListSYS_Role()
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * from SYS_Role";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			 List<SYS_Role> result = new  List<SYS_Role>();
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				while (dataReader.Read())
				{
					SYS_Role obj = GetSYS_RoleFromDataReader( dataReader );
					if(obj != null)
						result.Add(obj);
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("ListSYS_Role:" + ex.Message);
			return null;
		}
	}
	
}
#endregion